using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;


namespace RUPS
{
    public partial class A_NivelStocuri : Form
    {
        public int idTipMat;
        
        public A_NivelStocuri()
        {
            InitializeComponent();
        }

        private void A_NivelStocuri_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nivelStocuriDataSet.nivelStocuriW' table. You can move, or remove it, as needed.
          
            // TODO: This line of code loads data into the 'nivelStocuriDataSet.nivelStocuriW' table. You can move, or remove it, as needed.
            this.nivelStocuriWTableAdapter.Fill(this.nivelStocuriDataSet.nivelStocuriW);
            

        }

        private void a_nivMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8)
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

        private void revocaNivMin_Click(object sender, EventArgs e)
        {
            a_material.Text = "";
            a_nivMin.Text = "";


        }

        private void seteaza_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conexiune = nivelStocuriWTableAdapter.Connection;
                conexiune.Open();

                SqlCommand comanda = new SqlCommand("update tipMaterial set nivMinim = " + int.Parse(a_nivMin.Text) +
                    " where id = " + idTipMat,conexiune );
                comanda.ExecuteNonQuery();

                nivelStocuriWBindingSource.ResetBindings(false);
                nivelStocuriDataSetTableAdapters.nivelStocuriWTableAdapter dta = new RUPS.nivelStocuriDataSetTableAdapters.nivelStocuriWTableAdapter();
                nivelStocuriDataSet.nivelStocuriWDataTable tb = new nivelStocuriDataSet.nivelStocuriWDataTable();
                dta.Fill(tb);

                nivelStocuriWBindingSource.DataSource = tb;
                gridNivelStocuri.DataSource = null;
                gridNivelStocuri.DataSource = nivelStocuriWBindingSource;
                gridNivelStocuri.Invalidate();
                conexiune.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void gridNivelStocuri_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridViewRow rand = gridNivelStocuri.SelectedRows[0];
                idTipMat = int.Parse(rand.Cells[1].Value.ToString());

                a_material.Text = rand.Cells[0].Value.ToString();
                a_nivMin.Text = rand.Cells[2].Value.ToString();

              if(int.Parse(rand.Cells[2].Value.ToString()) < int.Parse(rand.Cells[3].Value.ToString()))
                  statusMaterial.Text = "Atentie!!!Nivel de alarma, trebuie facuta comanda";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void gridNivelStocuri_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            A_ComandaMateriale com = new A_ComandaMateriale();
            com.Show();
        }
    }
}